﻿#ifndef TableModel_H
#define TableModel_H
#include<QAbstractTableModel>
#include <QObject>
#include<QVector>
#include<QMap>
#include<QStringList>


//自定义QTableView model
class TableModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit TableModel(QObject *parent=nullptr);

    void SetData(const QVariantMap &map);
    void setHignlightIndex(QVector< QPair<int, int> > vec_index);

    //itemFlag相关的设置是针对项的，与View中的拖放、编辑、选择等属性的设置是独立的，一个项能否进行拖放、编辑、选择等
    virtual Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

    //行列数
    virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
    virtual int columnCount(const QModelIndex &parent) const;

    virtual QVariant data(const QModelIndex &index, int role) const;
    virtual bool setData(const QModelIndex &index, const QVariant &value, int role);

    QString itemText(int row, int column) const;
    QString itemText(const QModelIndex &index) const;
    void setItemText(const QModelIndex &index, const QString &str);
    void setItemText(int row, int column, const QString &str);

    QVariant ItemData(const QModelIndex &index) const;
    QVariant ItemData(int row, int column) const;
    void SetItemData(const QModelIndex &index, const QVariant &data);
    void SetItemData(int row, int column, const QVariant &data);

    void clear();

signals:
    void itemChanged(const QModelIndex &index, const QVariant &value);


private:
    QStringList m_pHeaders;

    QStringList m_hor_hedlbls;                      // 橫向标题
    QStringList m_vec_hedlbls;                      // 纵向标题
    QVariantMap m_table_map, m_data_map;
    QVector<QPair<int, int> > m_highlight_indexs;   // 背景高亮的indexs

};

#endif // TableModel_H
